package com.originskyseed.platform.course.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.course.mapper.CourseMapper;
import com.originskyseed.platform.course.mapper.CourseRelateAnnexMapper;
import com.originskyseed.platform.course.mapper.CourseRelateIsReadMapper;
import com.originskyseed.platform.course.mapper.CourseRelateRightMapper;
import com.originskyseed.platform.course.model.Course;
import com.originskyseed.platform.course.model.CourseRelateAnnex;
import com.originskyseed.platform.course.model.CourseRelateIsRead;
import com.originskyseed.platform.course.model.CourseRelateRight;
import com.originskyseed.platform.course.model.TaskSelectCourseVO;
import com.originskyseed.platform.course.service.ICourseService;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 课程表Service接口实现类（OSI_COURSE）
 *
 * @author Tangwanghui
 */
@DubboService
@Component("courseService")
public class CourseServiceImpl implements ICourseService {

	@Resource
	private CourseMapper courseMapper;

	@Resource
	private CourseRelateAnnexMapper courseRelateAnnexMapper;

	@Resource
	private CourseRelateRightMapper courseRelateRightMapper;

	@Resource
	private CourseRelateIsReadMapper courseRelateIsReadMapper;

	@Resource
	private UserInfoMapper userInfoMapper;
	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.courseMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.courseMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 批量逻辑删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateDelByIds(List<String> ids) throws Exception {
		return this.courseMapper.updateDelByIds(ids);
	}

	/**
	 * 批量修改状态
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateStatusByIds(Map<String, Object> map) throws Exception {
		return this.courseMapper.updateStatusByIds(map);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(Course record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.courseMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(Course record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.courseMapper.insertSelective(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelectiveAndFile(Course record, List<CourseRelateRight> courseRelateRightList, List<CourseRelateAnnex> courseRelateAnnexList) throws Exception {
		String key = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
		record.setId(key);
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		if (courseRelateAnnexList != null && courseRelateAnnexList.size() > 0) {
			for (CourseRelateAnnex courseRelateAnnex : courseRelateAnnexList) {
				courseRelateAnnex.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				courseRelateAnnex.setCourseId(record.getId());
				courseRelateAnnex.setType(record.getViewRange());
				courseRelateAnnex.setCreateTime(new Date());
				this.courseRelateAnnexMapper.insertSelective(courseRelateAnnex);
			}
		}

		if (courseRelateRightList != null && courseRelateRightList.size() > 0) {
			for (CourseRelateRight courseRelateRight : courseRelateRightList) {
				courseRelateRight.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				courseRelateRight.setCourseId(record.getId());
				courseRelateRight.setType(record.getViewRange());
				courseRelateRight.setCreateTime(new Date());
				this.courseRelateRightMapper.insertSelective(courseRelateRight);
			}
		}
		//阅读情况
		List<CourseRelateIsRead> list = new ArrayList<>();
        if(record.getBelongPlatform() != null){
        	// 如果是全部人员可看
        	if(record.getViewRange()==0){
				List<UserInfo> userInfos = this.selectUser(record.getBelongPlatform());
				if(userInfos != null && userInfos.size() > 0){
					for (UserInfo userInfo : userInfos) {
						CourseRelateIsRead courseRelateIsRead = new CourseRelateIsRead();
						courseRelateIsRead.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
						courseRelateIsRead.setCourseId(key);
						courseRelateIsRead.setIsRead(2);
						courseRelateIsRead.setCreateTime(new Date());
						courseRelateIsRead.setObjId(userInfo.getId());
						courseRelateIsRead.setObjName(userInfo.getName());
						list.add(courseRelateIsRead);
					}
				}
				// 指定范围
			}else {
				if (courseRelateRightList != null && courseRelateRightList.size() > 0) {
					for (CourseRelateRight courseRelateRight : courseRelateRightList) {
						CourseRelateIsRead courseRelateIsRead = new CourseRelateIsRead();
						courseRelateIsRead.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
						courseRelateIsRead.setCourseId(key);
						courseRelateIsRead.setIsRead(2);
						courseRelateIsRead.setCreateTime(new Date());
						courseRelateIsRead.setObjId(courseRelateRight.getObjId());
						courseRelateIsRead.setObjName(courseRelateRight.getObjName());
						list.add(courseRelateIsRead);
					}
				}
			}
			if(list != null && list.size() > 0){
				this.courseRelateIsReadMapper.insertBatch(list);
			}
		}

		return this.courseMapper.insertSelective(record);
	}

	/**
	 * 查询人员
	 *
	 * @param sys
	 * @throws Exception
	 */
	private List<UserInfo> selectUser(int sys) throws Exception {
		List<UserInfo> list = new ArrayList<>();
		UserInfo userInfo1 = new UserInfo();
		userInfo1.setIsDel(0);
		userInfo1.setStatus(1);
		if (sys == 0) {
			userInfo1.setSourceSys(0);
		} else if (sys == 1) {
			userInfo1.setSourceSys(1);
		} else if (sys == 2) {
			userInfo1.setSourceSys(2);
		}
		return this.userInfoMapper.selectList(userInfo1);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(Course record) throws Exception {
		record.setUpdateTime(new Date());
		return this.courseMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateAndFile(Course record, List<CourseRelateRight> courseRelateRightList) throws Exception {
		if (StringUtils.isNotEmpty(record.getCourseVideo())) {
			this.courseRelateAnnexMapper.deleteByCourseId(record.getId());
			String[] courseVideos = record.getCourseVideo().split(",");
			for (String video : courseVideos) {
				CourseRelateAnnex courseRelateAnnex = new CourseRelateAnnex();
				courseRelateAnnex.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				courseRelateAnnex.setCourseId(record.getId());
				courseRelateAnnex.setPath(video);
				courseRelateAnnex.setType(1);
				courseRelateAnnex.setCreateTime(new Date());
				this.courseRelateAnnexMapper.insertSelective(courseRelateAnnex);
			}
		}

		// 修改为查看全部，则删除绑定关系
		if(record.getViewRange()!=null && record.getViewRange()==0){
			this.courseRelateRightMapper.deleteByCourseId(record.getId());
		}
		if (courseRelateRightList != null && courseRelateRightList.size() > 0) {
			this.courseRelateRightMapper.deleteByCourseId(record.getId());
			for (CourseRelateRight courseRelateRight : courseRelateRightList) {
				courseRelateRight.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				courseRelateRight.setCourseId(record.getId());
				courseRelateRight.setType(record.getViewRange());
				courseRelateRight.setCreateTime(new Date());
				this.courseRelateRightMapper.insertSelective(courseRelateRight);
			}
		}
		record.setUpdateTime(new Date());
		return this.courseMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(Course record) throws Exception {
		record.setUpdateTime(new Date());
		return this.courseMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Course selectByPrimaryKey(String id) throws Exception {
		return this.courseMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Course> selectList(Course record) throws Exception {
		return this.courseMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Course> selectListByPage(Map<String, Object> params, BasePage<Course> page) throws Exception {
		params.put("page", page);
		List<Course> list = this.courseMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Course> selectListRelStudyByPage(Map<String, Object> params, BasePage<Course> page) throws Exception {
		params.put("page", page);
		List<Course> list = this.courseMapper.selectListRelStudyByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询课程id是否存在
	 *
	 * @param courseId
	 * @return
	 * @throws Exception
	 */
	public String selectCourseId(String courseId) throws Exception {
		return this.courseMapper.selectCourseId(courseId);
	}

	/**
	 * 查询最大的优先级
	 *
	 * @return
	 * @throws Exception
	 */
	public Integer selectPrority() throws Exception {
		return this.courseMapper.selectPrority();
	}

	/**
	 * 更新置顶信息
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsTop(Course record) throws Exception {
		record.setUpdateTime(new Date());
		this.courseMapper.updateIsTop(record);
		return this.courseMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 根据优先级查询课程
	 *
	 * @param prority
	 * @return
	 * @throws Exception
	 */
	@Override
	public Course selectCourseByPrority(Integer prority) throws Exception {
		return this.courseMapper.selectCourseByPrority(prority);
	}

	/**
	 * 更新排序信息
	 *
	 * @param downCourse
	 * @param upCourse
	 * @return
	 * @throws Exception
	 */
	public int updatePrority(Course downCourse, Course upCourse) throws Exception {
		downCourse.setUpdateTime(new Date());
		upCourse.setUpdateTime(new Date());
		this.courseMapper.updateByPrimaryKeySelective(downCourse);
		return this.courseMapper.updateByPrimaryKeySelective(upCourse);
	}

	/**
	 * 分页查询用户课程列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Course> selectUserCourseByPage(Map<String, Object> params, BasePage<Course> page) throws Exception {
		params.put("page", page);
		List<Course> list = this.courseMapper.selectUserCourseByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 分页查询用户课程列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public List<Course> selectUserStudySituationList(Map<String, Object> params) throws Exception {
		return this.courseMapper.selectUserStudySituationList(params);
	}

	/**
	 * 根据优先级查询状态
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public Integer selectStatusByPrority(Map<String, Object> params) throws Exception {
		return this.courseMapper.selectStatusByPrority(params);
	}

	/**
	 * 任务发布课程选择列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<TaskSelectCourseVO> selectTaskCourseByPage(Map<String, Object> params, BasePage<TaskSelectCourseVO> page) throws Exception {
		params.put("page", page);
		List<TaskSelectCourseVO> list = this.courseMapper.selectTaskCourseByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询课程分类是否存在
	 *
	 * @param courseTypeId
	 * @return
	 * @throws Exception
	 */
	public String selectCourseTypeExist(String courseTypeId) throws Exception {
		return this.courseMapper.selectCourseTypeExist(courseTypeId);
	}
}
